package com.lingx.jt808.msg;

import com.lingx.jt808.IJT808MsgHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class Msg0102 extends AbstrctMsgHandler implements IJT808MsgHandler {

    private static final Logger log = LoggerFactory.getLogger(Msg0102.class.getSimpleName());

    @Override
    public int getMsgId() {
        return 0x0102;
    }

    @Override
    public String getTag() {
        return "[0102]终端鉴权";
    }

    @Override
    public void handle(ByteBuf data, String tid, int msgId, int msgSn, ChannelHandlerContext ctx, boolean isVersion) {
        if (tid.trim().isEmpty()) {
            ctx.close();
            return;
        }
        String code;
        if (isVersion) {
            int len = data.readByte();
            byte bytes[] = new byte[len];
            data.readBytes(bytes);
            code = new String(bytes);
            //还有15字节的IMEI与20字节的软件版本号
        } else {
            byte bytes[] = new byte[data.readableBytes()];
            data.readBytes(bytes);
            code = new String(bytes);
        }

        boolean b = this.databaseService.handle0102(tid, code);
        if (!b) {
            ctx.close();
        }
    }
}
